﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xilium.CefGlue;

namespace IanWebkitBrowser
{
    public class IanBrowser : Control
    {
        public bool ScrollBarsEnabled { get; set; }
        private static object objectForScripting;
        public static object ObjectForScripting
        {
            get
            {
                return objectForScripting;
            }
            set
            {
                objectForScripting = value;
            }
        }

        public static App App { get; set; }

        public string HomeUrl { get; set; }

        public bool CanGoBack 
        {
            get
            {
                return App.Browser.CanGoBack;
            }
        }

        public IanBrowser()
        {
            
        }

        public delegate void Action();

        public void Release()
        {
            CefRuntime.Shutdown();
        }

        public void RunScript(string code)
        {
            App.Browser.GetMainFrame().ExecuteJavaScript(code, null, 0);
        }

        public void Navigate(string url)
        {
            App.Browser.GetMainFrame().LoadUrl(url);
        }

        public bool GoBack()
        { 
            if(CanGoBack)
            {
                App.Browser.GoBack();
            }
            return CanGoBack;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            //base.OnSizeChanged(e);
            try
            {
                IntPtr handle = App.Browser.GetHost().GetWindowHandle();
                if (handle != IntPtr.Zero)
                {
                    NativeMethods.SetWindowPos(handle, IntPtr.Zero,
                        0, 0, Width, Height,
                        SetWindowPosFlags.NoMove | SetWindowPosFlags.NoZOrder
                        );
                }
            }
            catch (Exception)
            { }
        }

        public void Init(string[] args)
        {
            CefRuntime.Load();
            App = new App();
            App.Args = new CefMainArgs(args);
            CefSettings setting = new CefSettings();
            setting.SingleProcess = true;
            setting.MultiThreadedMessageLoop = CefRuntime.Platform == CefRuntimePlatform.Windows;
            setting.LogSeverity = CefLogSeverity.Verbose;
            setting.LogFile = "cef.log";
            setting.ResourcesDirPath = System.IO.Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase).LocalPath);
            setting.RemoteDebuggingPort = 20480;
            setting.NoSandbox = true;

            int ret = CefRuntime.ExecuteProcess(App.Args, App, IntPtr.Zero);
            if (ret == -1)
            {
                CefRuntime.Initialize(App.Args, setting, App, IntPtr.Zero);
                CefWindowInfo windowInfo = CefWindowInfo.Create();
                windowInfo.SetAsChild(Handle, new CefRectangle(0, 0, Width, Height));
                App.Client = new Client(App);
                CefBrowserSettings brSetting = new CefBrowserSettings();
                CefBrowserHost.CreateBrowser(windowInfo, App.Client, brSetting, HomeUrl);
            }
        }
    }
}
